package org.dru.clay.rhino.feature.googlecode;

import org.dru.clay.google.code.upload.GoogleCodeUploader;
import org.dru.clay.rhino.feature.Feature;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;

/**
 * GoogleCode
 * User: joakimd
 * Date: 2013-07-18
 * Time: 17:46
 */
public class GoogleCode extends Feature {
    public static final String[] FUNCTION_NAMES = {
            "GoogleCode_upload"
    };

    @Override
    public void init(final Context context, final ScriptableObject scope) throws Exception {
        scope.defineFunctionProperties(FUNCTION_NAMES, GoogleCode.class, ScriptableObject.DONTENUM);
        loadFeatureScript(context, scope, GoogleCode.class);
    }

    public static void GoogleCode_upload(final Context context, final Scriptable thisObj,
                                         final Object[] args, final Function funcObj) throws Exception {
        final ScriptableObject config = (ScriptableObject) args[0];
        final GoogleCodeUploader uploader = new GoogleCodeUploader();
        uploader.setUserName(Context.toString(config.get("userName")));
        uploader.setPassword(Context.toString(config.get("password")));
        uploader.setProjectName(Context.toString(config.get("projectName")));
        uploader.setFileName(Context.toString(config.get("fileName")));
        uploader.setTargetFileName(Context.toString(config.get("targetFileName")));
        uploader.setSummary(Context.toString(config.get("summary")));
        if (ScriptableObject.hasProperty(config, "uploadUrl") && config.get("uploadUrl") != Context.getUndefinedValue()) {
//            uploader.setUploadUrl(Context.toString(config.get("uploadUrl")));
        }
        uploader.setVerbose(Context.toBoolean(config.get("verbose")));
        uploader.setLabels(Context.toString(config.get("labels")));
        uploader.setIgnoreSslCertificateHostname(Context.toBoolean(config.get("ignoreSslCertificateHostname")));
        uploader.execute();
    }
}
